package com.ouheng.shop.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ouheng.common.utils.CommonUtils;
import com.ouheng.shop.dao.GzMemberDao;
import com.ouheng.shop.dao.LebeDao;
import com.ouheng.shop.dao.LebeiSetDao;
import com.ouheng.shop.dao.MemberDao;
import com.ouheng.shop.dao.MemberRelationDao;
import com.ouheng.shop.dao.OhOrderDao;
import com.ouheng.shop.dao.RedcodeDao;
import com.ouheng.shop.domain.GzMemberDO;
import com.ouheng.shop.domain.LebeDO;
import com.ouheng.shop.domain.LebeiSetDO;
import com.ouheng.shop.domain.MemberDO;
import com.ouheng.shop.domain.MemberRelationDO;
import com.ouheng.shop.domain.OhOrderDO;
import com.ouheng.shop.domain.RedcodeDO;
import com.ouheng.shop.domain.ShopVoucherDO;
import com.ouheng.shop.service.OhOrderService;
import com.ouheng.wxgz.WxGzpush;



@Service
public class OhOrderServiceImpl implements OhOrderService {
	@Autowired
	private OhOrderDao ohOrderDao;
	
	@Autowired
	private MemberDao memberDao;
	
	@Autowired
	private RedcodeDao redcodeDao;
	
	@Autowired
	private LebeDao lebeDao;
	
	@Autowired
	private GzMemberDao gzMemberDao;
	
	@Autowired
    private SimpMessagingTemplate template;
	
	@Override
	public OhOrderDO get(Long id){
		return ohOrderDao.get(id);
	}
	
	@Override
	public List<OhOrderDO> list(Map<String, Object> map){
		return ohOrderDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return ohOrderDao.count(map);
	}
	
	@Override
	public int save(OhOrderDO ohOrder){
		return ohOrderDao.save(ohOrder);
	}
	
	@Override
	public int update(OhOrderDO ohOrder){
//		template.convertAndSendToUser(ohOrder.getUser().toString(), "/queue/notifications", "新消息：" + "1111");
		OhOrderDO ohOrderDO = ohOrderDao.get(ohOrder.getId());
		MemberDO memberDO = memberDao.get(ohOrderDO.getMemberid());
		if(StringUtils.isNotBlank(memberDO.getUnionid())&&"2".equals(ohOrderDO.getStatus())){
			GzMemberDO gzMemberDO = gzMemberDao.getByUnionid(memberDO.getUnionid());
			if(gzMemberDO!=null){
				Map<String,Object> first=new HashMap<String, Object>();
				first.put("value", "您的订单已发货，邮差正快马加鞭速递中:");
				first.put("color", "#173177");
				
				Map<String,Object> keyword1=new HashMap<String, Object>();
				keyword1.put("value", "奈斯情趣家庭幸福生活用品");
				keyword1.put("color", "#173177");
				
				
				Map<String,Object> keyword2=new HashMap<String, Object>();
				keyword2.put("value", ohOrder.getLogisticsname());
				keyword2.put("color", "#173177");
				
				
				Map<String,Object> keyword3=new HashMap<String, Object>();
				keyword3.put("value", ohOrder.getLogisticsnumber());
				keyword3.put("color", "#173177");
				

				Map<String,Object> keyword4=new HashMap<String, Object>();
				keyword4.put("value", ohOrderDO.getAddress());
				keyword4.put("color", "#173177");
				

				Map<String,Object> remark=new HashMap<String, Object>();
				remark.put("value", "亲爱的客户，您的订单已派送！");
				remark.put("color", "#173177");
				
				Map<String,Object> data=new HashMap<String, Object>();
				
				data.put("first", first);
				data.put("keyword1", keyword1);
				data.put("keyword2", keyword2);
				data.put("keyword3", keyword3);
				data.put("keyword4", keyword4);
				data.put("remark", remark);
				
				Map<String,Object> m=new HashMap<String, Object>();
				m.put("data", data);
				
				m.put("touser", gzMemberDO.getWopenid());
				
				m.put("template_id", "8HBbQssFKFj454aL304iuvHQBcKIoAFudNJJAJ1_-H4");
				
				WxGzpush.push(m);
			}
		}
		if("2".equals(ohOrderDO.getStatus())){
			ohOrder.setModifydate(new Date());
		}
		return ohOrderDao.update(ohOrder);
	}
	
	@Override
	public int remove(Long id){
		return ohOrderDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return ohOrderDao.batchRemove(ids);
	}

	@Override
	public String orderProductNameAndNumber(Long id) {
		return ohOrderDao.orderProductNameAndNumber(id);
	}

	@Override
	public List<OhOrderDO> listOhOrderByIds(Long[] ids) {
		return ohOrderDao.listOhOrderByIds(ids);
	}

	@Override
	public List<OhOrderDO> listReturnMoeny(Map<String, Object> map) {
		return ohOrderDao.listReturnMoeny(map);
	}

	@Override
	public int returnMoenyCount(Map<String, Object> map) {
		return ohOrderDao.returnMoenyCount(map);
	}

	@Override
	@Transactional
	public int finish(Long id) {
		OhOrderDO order = ohOrderDao.get(id);
		if(order!=null){
			Map<String, Object> map = new HashMap<>();
			map.put("orderid", id);
			map.put("state", "1");
			List<RedcodeDO> listRedcode = redcodeDao.list(map);
			//红包
			if(listRedcode.size()>0&&listRedcode!=null){
				RedcodeDO redcodeDO = listRedcode.get(0);
				redcodeDO.setState("2");
				redcodeDO.setModifydate(new Date());
				MemberDO member = memberDao.get(redcodeDO.getMemberid());
				if(member!=null){
					BigDecimal sum =member.getRedenvelope().add(redcodeDO.getRedmoney());
					member.setRedenvelope(sum);
					memberDao.update(member);
					if(StringUtils.isNotBlank(member.getUnionid())){
						GzMemberDO gzMemberDO = gzMemberDao.getByUnionid(member.getUnionid());
						if(gzMemberDO!=null){
							Map<String,Object> first=new HashMap<String, Object>();
							first.put("value", "尊敬的"+member.getNickname()+",佣金已充入您的账户中");
							first.put("color", "#173177");
							
							Map<String,Object> keyword1=new HashMap<String, Object>();
							keyword1.put("value", order.getNumber());
							keyword1.put("color", "#173177");
							
							
							Map<String,Object> keyword2=new HashMap<String, Object>();
							keyword2.put("value", "￥"+redcodeDO.getRedmoney());
							keyword2.put("color", "#173177");
							

							Map<String,Object> remark=new HashMap<String, Object>();
							remark.put("value", "点击查看账户");
							remark.put("color", "#173177");
							
							Map<String,Object> data=new HashMap<String, Object>();
							
							data.put("first", first);
							data.put("order", keyword1);
							data.put("money", keyword2);
							data.put("remark", remark);
							
							Map<String,Object> m=new HashMap<String, Object>();
							m.put("data", data);
							
							m.put("touser", gzMemberDO.getWopenid());
							
							m.put("template_id", "xWVKK9l0vDz_2wGgjfVz6t-juc6WX2gSO7aKPylbHew");
							
							WxGzpush.push(m);
						}
					}
				}
				redcodeDao.update(redcodeDO);
			}
			Map<String, Object> map1 = new HashMap<>();
			map1.put("orderid", id);
			map1.put("state", "1");
			List<LebeDO> lebelist = lebeDao.list(map1);
			//有币
			if(lebelist.size()>0&&lebelist!=null){
				LebeDO lebeDO = lebelist.get(0);
				lebeDO.setState("2");
				lebeDO.setModifydate(new Date());
				MemberDO member = memberDao.get(lebeDO.getMemberid());
				if(member!=null){
					BigDecimal aa = member.getLebe().add(order.getPaymoney());
					member.setLebe(aa);
				}
				memberDao.update(member);
				lebeDao.update(lebeDO);
			}
			order.setState("2");
			if("3".equals(order.getStatus())){
				order.setStatus("6");
				order.setCompletedate(new Date());
			}
			int sum = ohOrderDao.update(order);
			return sum;
		}
		return 0;
	}

	@Override
	public List<OhOrderDO> listOrderByMemberid(Map<String,Object> map) {
		return ohOrderDao.listOrderByMemberid(map);
	}

	@Override
	public int listOrderByMemberidCount(Map<String,Object> map) {
		return ohOrderDao.listOrderByMemberidCount(map);
	}

	@Override
	public List<OhOrderDO> listOrder(Map<String, Object> map) {
		return ohOrderDao.listOrder(map);
	}

	@Override
	public List<OhOrderDO> listByStatus(Map<String, Object> map) {
		return ohOrderDao.listByStatus(map);
	}

	@Override
	public int countByStatus(Map<String, Object> map) {
		return ohOrderDao.countByStatus(map);
	}
	
}
